/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-04-11
 * Time:23:13
 */
import java.util.Stack;

/**
 * JZ9 用两个栈实现队列
 */
public class Thequeueisimplementedwithtwostacks {
    public static Stack<Integer> stack1 = new Stack<Integer>();
    public static Stack<Integer> stack2 = new Stack<Integer>();

    public static void push(int node) {
        stack1.push(node);
    }

    public static int pop() {
//        while (stack1.empty()){
//            int num = stack1.pop();
//            stack2.push(num);
//        }
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public static void main(String[] args) {
        Thequeueisimplementedwithtwostacks queue = new Thequeueisimplementedwithtwostacks();

        // 插入元素1、2、3
        queue.push(1);
        queue.push(2);
        queue.push(3);

        // 删除元素1
        int front = queue.pop();
        System.out.println("删除的元素为：" + front);

        // 插入元素4、5
        queue.push(4);
        queue.push(5);

        // 删除元素2、3、4、5
        while (!isEmpty(queue)) {
            front = queue.pop();
            System.out.println("删除的元素为：" + front);
        }
    }

    private static boolean isEmpty(Thequeueisimplementedwithtwostacks queue) {
        return queue.pop() == -1; // -1表示队列为空
    }
}

